package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.InputVerifier;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

/**
 * A convenient encapsulation of required JComponents required
 * to gather information about the geological cross section.
 * @author Stephan
 *
 */
@SuppressWarnings("serial")
public class CrossSectionPanel extends JPanel
{	
	/**
	 * The ground type that this panel is representing
	 */
	private String groundType;
	
	/**
	 * The depth of this cross section.
	 */
	private JTextField depth;
	/**
	 * The units of this cross section
	 */
	private JLabel units;
	
	/**
	 * 
	 */
	private JCheckBox checkBox;
	
	/**
	 * Initializes this particular instance of the <code>CrossSectionPanel</code>
	 * @param groundType the ground type of this panel.
	 */
	public CrossSectionPanel(String groundType)
	{
//		super(new BorderLayout(0,50));
		super();
		
		this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
		
		this.groundType = groundType;
		setBorder(new LineBorder(Color.BLACK));
		
		depth = new JTextField();
		depth.setInputVerifier(new DoubleVerifier());
		
		depth.setPreferredSize(new Dimension(50, depth.getPreferredSize().height));
		
		units = new JLabel("feet"); // TODO: Display current/correct unit
		units.setPreferredSize(new Dimension(50, 26));
		units.setBorder(new EmptyBorder(0, 5, 0, 0));
		
		checkBox = new JCheckBox();
		checkBox.setPreferredSize(new Dimension(65, checkBox.getPreferredSize().height));
		checkBox.setText(groundType);
		
		this.add(checkBox, BorderLayout.LINE_START);
		this.add(depth, BorderLayout.CENTER);
		this.add(units, BorderLayout.LINE_END);
	}
	
	/**
	 * Gets the ground type represented by this <code>CrossSectionPanel</code>
	 * @return the ground type of this <code>CrossSectionPanel</code>
	 */
	public String getGroundType()
	{
		return groundType;
	}
	
	/**
	 * @return true if checked, false otherwise
	 */
	public boolean isChecked()
	{
		return checkBox.isSelected();
	}
	
	/**
	 * Gets the depth of this layer of the cross section
	 * @return the depth of this layer of the cross section.
	 */
	public double getDepth()
	{
		try
		{
			return Double.parseDouble(depth.getText());
		}
		catch(NumberFormatException e)
		{
			return 0;
		}
	}
	
	/**
	 * Sets the depth of this layer of the cross section
	 * @param pDepth the new depth of this cross section.
	 */
	public void setDepth(double pDepth)
	{
		depth.setText(String.valueOf(pDepth));
	}
	
	/**
	 * @see Object#toString()
	 */
	@Override
	public String toString()
	{
		return groundType;
	}
	
	/**
	 * @see javax.swing.JComponent#getPreferredSize()
	 **/
	@Override
	public Dimension getPreferredSize()
	{
		Dimension dim = new Dimension(0, 0);
		
		for(Component c : this.getComponents())
		{
			dim.width += c.getPreferredSize().width;
			dim.height = Math.max(dim.height, c.getPreferredSize().height);
//			System.out.println(c + "\n\t" + c.getPreferredSize());
		}
		
		return dim;
	}
	
	
	/** 
	 * @see javax.swing.JComponent#getMaximumSize()
	 **/
	@Override
	public Dimension getMaximumSize()
	{
		Dimension dim = getPreferredSize();
		dim.width = Integer.MAX_VALUE;
		return dim;
	}

	/**
	 * Input verifier that will guarantee that the user has entered only 
	 * valid data
	 * @author Stephan
	 *
	 */
	class DoubleVerifier extends InputVerifier
	{
		/**
		 * @see javax.swing.InputVerifier#verify(javax.swing.JComponent)
		 */
		// Return true if there is a valid double that is greater than or equal to 0
		@Override
		public boolean verify(JComponent input)
		{
			if(((JTextField)input).getText().equals(""))
				return true;
			try
			{
				double d = Double.parseDouble(((JTextField)input).getText());
				return d > 0;
			}
			catch(NumberFormatException e)
			{
				return false;
			}
		}
		
		/**
		 * @see javax.swing.InputVerifier#shouldYieldFocus(javax.swing.JComponent)
		 */
		// Provides a visual indication that the user has f*ed up
		@Override
		public boolean shouldYieldFocus(JComponent input)
		{
			boolean retval = super.shouldYieldFocus(input);
			
			if(retval)
			{
				input.setBackground(Color.WHITE);
				input.setToolTipText(null);
			}
			else
			{
				input.setBackground(Color.YELLOW);
				input.setToolTipText("You must enter a value greater than 0.");
			}
			
			return retval;
		}
		
	}
}
